package com.alan.java8;

import com.alan.java8.rule.AppleGreenColorPredicate;
import com.alan.java8.rule.ApplePredicate;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Function;
import java.util.function.Predicate;

/**
 * Lambda
 * Created by Alan on 2017/10/6.
 */
public class java8demo {
    private static List<Apple> repos = Arrays.asList(
        new Apple(120,"red"),
        new Apple(110,"green"),
        new Apple(130,"blue"),
        new Apple(90,"red")
    );
    public static void main(String[] args) {
        //List<Apple> greenApples = filterApples(repos, (Apple apple)->"red".equals(apple.getColor()));
        //List<Apple> greenApples = filterApples(repos, (Apple apple)->{return "red".equals(apple.getColor());});
//        List<Apple> greenApples = filterApples2(repos, (Apple apple)->{return "red".equals(apple.getColor());});
//        System.out.println(greenApples);

        String firstName = "";
        String lastName = "mic";
        System.out.println(validate(firstName,name->name.isEmpty()?"名字不能为空！":name));
        System.out.println(validate(lastName,name->name.isEmpty()?"名字不能为空！":name));
    }

    private static String validate(String name, Function<String,String> function){
        return function.apply(name);
    }

    private static List<Apple> filterApples2(List<Apple> repos, Predicate<Apple> predicate){
        List<Apple> result = new ArrayList<>();
        for (int i = 0; i < repos.size();i++){
            Apple apple = repos.get(i);
            if(predicate.test(apple)){
                result.add(apple);
            }
        }
        return result;
    }

    private static List<Apple> filterApples(List<Apple> repos, ApplePredicate applePredicate){
        List<Apple> result = new ArrayList<>();
        for (int i = 0; i < repos.size();i++){
            Apple apple = repos.get(i);
            if(applePredicate.test(apple)){
                result.add(apple);
            }
        }
        return result;
    }

}
